package com.baizhi.controller;

import com.baizhi.entity.Album;
import com.baizhi.entity.Article;
import com.baizhi.lucene.LuceneService;
import com.baizhi.service.AlbumService;
import com.baizhi.service.AtricleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Administrator on 2019/4/3.
 */
@Controller
@RequestMapping("/Article")
public class AtricleController {
    @Autowired
    private AtricleService atricleService;
    @Autowired
    private LuceneService luceneService;



    @RequestMapping("/selectAllArticle.do")
    @ResponseBody
    public Map selectAllArticle(int rows,int page,String atricleName){
        if(atricleName==null ||  "".equals(atricleName)){
            return atricleService.selectAllArticle(rows,page,atricleName);
        }else{
            List<Article> articles = luceneService.queryByKeyword(atricleName);
            int count=articles.size();
            Map map=new HashMap();
            map.put("total",count);
            map.put("rows",articles);
            return map;
        }
    };


    @ResponseBody
    @RequestMapping("/addArticle.do")
    public Map addArticle(HttpServletRequest request, Article article, MultipartFile Image){
        if(Image!=null){
            String fileName=Image.getOriginalFilename();
            String st = UUID.randomUUID().toString();
            String newName=st+fileName;
            String realPathNewMethod = request.getServletContext().getRealPath("img/si");
            try {
                Image.transferTo(new File(realPathNewMethod+"/"+newName));
            } catch (IllegalStateException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            article.setArticleImage("/si/"+newName);
            return  atricleService.addArticle(article);
        }else {
            article.setArticleImage("/si/"+"1.jpg");
            return  atricleService.addArticle(article);
        }
    }






    @ResponseBody
    @RequestMapping("/delateArticle.do")
    public Map delateArticle(Integer[] ids){
        return  atricleService.delateArticle(ids);
    }



    @ResponseBody
    @RequestMapping("/updateArticle.do")
    public Map updateArticle(HttpServletRequest request,Article article, MultipartFile albumImage){
        if(albumImage!=null){
            String fileName=albumImage.getOriginalFilename();
            String st = UUID.randomUUID().toString();
            String newName=st+fileName;
            String realPathNewMethod = request.getServletContext().getRealPath("img/si");
            try {
                albumImage.transferTo(new File(realPathNewMethod+"/"+newName));
            } catch (IllegalStateException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            article.setArticleImage("/si/"+newName);
            return  atricleService.updateArticle(article);
        }else {
            article.setArticleImage("/si/"+"1.jpg");
            return  atricleService.updateArticle(article);
        }

    }



}
